//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.amado.course53EncodeDecode;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.*;

public class TigerSignature {
    private static final String PRIVATE_KEY_BEGIN = "-----BEGIN PRIVATE KEY-----";
    private static final String PRIVATE_KEY_END = "-----END PRIVATE KEY-----";

    public TigerSignature() {
    }

    public static String rsaSign(String content, String privateKey, String charset) {
        if (privateKey.startsWith("-")) {
            if (privateKey.contains("-----BEGIN PRIVATE KEY-----")) {
                privateKey = privateKey.replace("-----BEGIN PRIVATE KEY-----", "");
            }

            if (privateKey.contains("-----END PRIVATE KEY-----")) {
                privateKey = privateKey.replace("-----END PRIVATE KEY-----", "");
            }
        }

        try {
            PrivateKey priKey = getPrivateKey("RSA", new ByteArrayInputStream(privateKey.getBytes()));
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initSign(priKey);
            if (charset==null) {
                signature.update(content.getBytes());
            } else {
                signature.update(content.getBytes(charset));
            }

            byte[] signed = signature.sign();
            return Base64.getEncoder().encodeToString(signed);
        } catch (Exception var6) {
            throw new RuntimeException(var6);
        }
    }

    public static PrivateKey getPrivateKey(String algorithm, InputStream ins) throws Exception {
        if (ins != null && algorithm!=null) {
            KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
            byte[] encodedKey = StreamUtil.readText(ins).getBytes();
            encodedKey = Base64.getDecoder().decode(encodedKey);
            return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
        } else {
            return null;
        }
    }

    public static boolean rsaCheckContent(String content, String sign, String publicKey, String charset)  {
        boolean rsaCheckContent = rsaCheck(content, sign, publicKey, charset);
        if (rsaCheckContent || !content.contains("\\/") && !content.contains("\\\"")) {
            return rsaCheckContent;
        } else {
            String source = content.replace("\\/", "/").replace("\\\"", "\"");
            return rsaCheck(source, sign, publicKey, charset);
        }
    }

    private static boolean rsaCheck(String content, String sign, String publicKey, String charset) {
        try {
            PublicKey pubKey = getPublicKey("RSA", new ByteArrayInputStream(publicKey.getBytes()));
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initVerify(pubKey);
            if (charset==null) {
                signature.update(content.getBytes());
            } else {
                signature.update(content.getBytes(charset));
            }

            return signature.verify(Base64.getDecoder().decode(sign.getBytes()));
        } catch (Exception var6) {
            throw new RuntimeException(var6);
        }
    }

    public static PublicKey getPublicKey(String algorithm, InputStream ins) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
        StringWriter writer = new StringWriter();
        StreamUtil.io(new InputStreamReader(ins), writer);
        byte[] encodedKey = writer.toString().getBytes();
        encodedKey = Base64.getDecoder().decode(encodedKey);
        return keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
    }

    public static void main(String[] args) {
        String strPrivateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJxwarz81K66uVtUNaUeAdH4nxaLF9z9gWbCTYSGUkOF84wz5/L8IGf7GSAp+J1YJXYVK1Y4unWI/A8Ic4BDjK+J3yp9LgtyUPHpGKrHhkcxYce0NIK3C8Ry2cuB4x0TOHYniD8rR45udoy6+XjyjetT4uCIqB+bCWdjuglJJislAgMBAAECgYAD6FbyM4qIdseMfXT9k9tNyF0I7h6BeZ+S1oWjKsvBPgZQWTeQ/TU+A6qkAAhivcHpyiuT8sTNjMdnBLoF2SoZalOSijmpjNnICnwQfEpvPjCTmIUPbqR9kc/cJhBXrh/ivZlIfYIqV8Uo1pGYx2xA7QocWrMbidOUWzE8Qi9LvQJBAN//7vN/O2LvqqBSkz7uPlgPCschVyu4KROcXbqZFoOv9CS7ZZ6D9RLSTR22f640VHqamwWanQ5v4N9+6btaEasCQQCyyawqaeH2xqHJ7iF8kJngE2+oUeMvYpYqAJoMBWnKjsjG5suqD8f4anKjSuUXJmWyzmMZG9wq12y/uHjwi4ZvAkAbMyK64po9e0jGywAiRh+/5aOzZnMFVgl23ifrfqdjOjx1MWQ3iWJ69Jp0nzKF3sjbUma8mXe0xxURToHnRBqdAkEAlO3pvGr3D9nma48x1dGeXEnUeSG9XXuCGvrlV54qwdCu7wDSJpd+vC/vzpZUvzP9AzVo10Gd5RIORKYmGDohgQJBANuiJ9Vyln1v2jRxWPEY80xRKj7LShBV5AbQr+D9hp3xeGHiy5oVybvCXjq+ktewe7PnNmX+13iaZQ0rL9/1n+4=";
        String strPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCccGq8/NSuurlbVDWlHgHR+J8Wixfc/YFmwk2EhlJDhfOMM+fy/CBn+xkgKfidWCV2FStWOLp1iPwPCHOAQ4yvid8qfS4LclDx6Riqx4ZHMWHHtDSCtwvEctnLgeMdEzh2J4g/K0eObnaMuvl48o3rU+LgiKgfmwlnY7oJSSYrJQIDAQAB";
        String content = "abc";
        String sign = rsaSign(content,strPrivateKey,null);
        System.out.println("sign"+sign);
        System.out.println(rsaCheckContent(content,sign,strPublicKey,null));
    }
}
